/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.daos;


import database.orm.ORMException;
import database.databasemanager.exceptions.DataBaseException;
import beans.ProviderBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


/**
 *
 * @author Fam. Tapia
 */
public class ProviderDAO extends AbstractDAO<ProviderBean> {

    @Override
    public void insert(ProviderBean provider) throws DAOException {
        try{
        dataBaseManager.openConnection();
        String sqlStatement = "INSERT INTO PROVIDER(providerRFC,providerName,providerPhone )values('"
                + provider.getProviderRFC()+"','"
                + provider.getProviderName()+"','"
                + provider.getProviderPhone()+"');";

            dataBaseManager.executeUpdate(sqlStatement);
              }
        catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }

    }

    @Override
    public void delete(String condition) throws DAOException {
        try{
        dataBaseManager.openConnection();
        String sqlStatement = "DELETE FROM provider " + condition;
            
                dataBaseManager.executeUpdate(sqlStatement);
             }
        catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override
    public void update(ProviderBean provider, String condition) throws DAOException {
        try{
        dataBaseManager.openConnection();
        String sqlStatement = "UPDATE provider SET providerId='"+provider.getProviderId()+
                         "',providerRFC='"+ provider.getProviderRFC()+
                         "',providerName='"+ provider.getProviderName()+
                         "',providerPhone='"+ provider.getProviderPhone()+"' "+condition;
             
                dataBaseManager.executeUpdate(sqlStatement);
             }
        catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override
    protected ArrayList<ProviderBean> searchAll(String condition) throws DAOException {
        try{
                dataBaseManager.openConnection();
                String sqlStatement = "SELECT * FROM PROVIDER "+ condition;
                ArrayList<ProviderBean> providerBeanList = null;
                providerBeanList = createProviderBeanList(dataBaseManager.executeQuery(sqlStatement));
                return providerBeanList;
          } catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }


    @Override
    protected ProviderBean search(String condition) throws DAOException {
        try{
                dataBaseManager.openConnection();
                String sqlStatement = "SELECT * FROM PROVIDER "+ condition;
                ProviderBean providerBean = null;
                providerBean = createProviderBean(dataBaseManager.executeQuery(sqlStatement));
                return providerBean;
          } catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }


 private ArrayList<ProviderBean> createProviderBeanList(ResultSet queryResult) throws DAOException{
     try{
        ArrayList<ProviderBean> providerBeansList = new ArrayList();
        while(queryResult.next()){
            providerBeansList.add((ProviderBean) beansGenerator.generateBean(queryResult, ProviderBean.class));
        }
        return providerBeansList;
         } catch (ORMException exception) {
             throw new DAOException(exception.getExceptionMessage(),exception);
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        }
    }

    private ProviderBean createProviderBean(ResultSet queryResult) throws DAOException{
        try {
            moveQueryResultToBegin(queryResult);
            return (ProviderBean) beansGenerator.generateBean(queryResult, ProviderBean.class);
        } catch (ORMException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        }
    }



     public void moveQueryResultToBegin(ResultSet queryResult) throws SQLException{
        queryResult.first();
    }

     public ProviderBean searchProvider(String providerRFC) throws DAOException{
         return search("WHERE providerRFC='"+ providerRFC +"'");
     }


}